USE [BS2]
GO
/****** Object:  Table [dbo].[Orders]    Script Date: 01/25/2015 22:37:22 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Orders](
	[OrderID] [bigint] IDENTITY(1,1) NOT NULL,
	[OrderDate] [smalldatetime] NOT NULL,
	[OrderNo] [varchar](16) NOT NULL,
	[SessionID] [smallint] NOT NULL,
	[Remarks] [varchar](150) NULL,
	[ReadOnly] [bit] NOT NULL CONSTRAINT [DF_Orders_ReadOnly1]  DEFAULT ((0)),
	[PartyID] [int] NULL,
	[BookedByID] [int] NULL,
	[LoginID] [int] NOT NULL CONSTRAINT [DF_Orders_LoginID]  DEFAULT ((1)),
	[HostName] [varchar](50) NULL CONSTRAINT [DF_Orders_HostName]  DEFAULT (host_name()),
	[EntryDateTime] [datetime] NULL CONSTRAINT [DF_Orders_EntryDateTime]  DEFAULT (getdate()),
	[ModifyID] [int] NULL CONSTRAINT [DF_Orders_LoginID1]  DEFAULT ((1)),
	[ModifyHostName] [varchar](50) NOT NULL CONSTRAINT [DF_Orders_HostName1]  DEFAULT (host_name()),
	[ModifyDateTime] [datetime] NOT NULL CONSTRAINT [DF_Orders_EntryDateTime1]  DEFAULT (getdate()),
 CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED 
(
	[OrderID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

-----------------------------------------------------------------------

USE [BS2]
GO
/****** Object:  Table [dbo].[OrderDetails]    Script Date: 01/25/2015 21:48:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[OrderDetails](
	[OrderDetailID] [bigint] IDENTITY(1,1) NOT NULL,
	[OrderID] [bigint] NOT NULL,
	[ItemID] [smallint] NULL,
	[PSize] [numeric](7, 2) NOT NULL CONSTRAINT [DF_OrderDetails_PSize]  DEFAULT ((0)),
	[Packs] [numeric](9, 2) NOT NULL CONSTRAINT [DF_OrderDetails_Packs]  DEFAULT ((0)),
	[Lose] [numeric](9, 2) NOT NULL CONSTRAINT [DF_OrderDetails_Lose]  DEFAULT ((0)),
	[Qty] [numeric](9, 2) NOT NULL CONSTRAINT [DF_OrderDetails_Qty]  DEFAULT ((0)),
	[PackRate] [numeric](9, 2) NOT NULL CONSTRAINT [DF_OrderDetails_PackRate]  DEFAULT ((0)),
	[Rate] [numeric](9, 2) NOT NULL CONSTRAINT [DF_OrderDetails_Rate]  DEFAULT ((0)),
	[Amount] [numeric](11, 2) NOT NULL CONSTRAINT [DF_OrderDetails_Amount]  DEFAULT ((0))
 CONSTRAINT [PK_OrderDetails] PRIMARY KEY CLUSTERED 
(
	[OrderDetailID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[OrderDetails]  WITH CHECK ADD  CONSTRAINT [FK_OrderDetails_Orders] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
GO
ALTER TABLE [dbo].[OrderDetails] CHECK CONSTRAINT [FK_OrderDetails_Orders]

----------------------------------------------------------------------------

USE [BS2]
GO
/****** Object:  View [dbo].[VW_Orders]    Script Date: 01/25/2015 22:57:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[VW_Orders]
AS
SELECT     dbo.Orders.OrderID, dbo.Orders.OrderDate, dbo.Orders.OrderNo, dbo.Orders.SessionID, dbo.SessionInfo.SessionTitle, dbo.SessionInfo.SessionFrom, 
                      dbo.SessionInfo.SessionTo, dbo.SessionInfo.CurrentSession, dbo.Orders.Remarks, dbo.Orders.ReadOnly, dbo.Orders.PartyID, 
                      dbo.Accounts.AccountTitle AS PartyName, dbo.Accounts.AccountTitleUrdu AS PartyNameUrdu, dbo.Accounts.Remarks AS PartyAddress, 
                      dbo.Accounts.CellNo AS PartyCellNo, dbo.Accounts.Phone AS PartyPhone, dbo.Orders.BookedByID, Accounts_1.AccountTitle AS BookedByName, 
                      Accounts_1.AccountTitleUrdu AS BookedByNameUrdu, Accounts_1.Remarks AS BookedByAddress, Accounts_1.CellNo AS BookedByCellNo, 
                      Accounts_1.Phone AS BookedByPhone, dbo.Orders.LoginID, dbo.Employees.EmployeeName AS LoginName, dbo.Orders.HostName, 
                      dbo.Orders.EntryDateTime, dbo.Orders.ModifyID, Employees_1.EmployeeName AS ModifyName, dbo.Orders.ModifyHostName, 
                      dbo.Orders.ModifyDateTime, dbo.OrderDetails.OrderDetailID, dbo.OrderDetails.ItemID, dbo.vw_Items.ItemCode, dbo.vw_Items.ItemName, 
                      dbo.vw_Items.ItemNameUrdu, dbo.vw_Items.GroupID, dbo.vw_Items.GroupCode, dbo.vw_Items.GroupName, dbo.vw_Items.GroupNameUrdu, 
                      dbo.vw_Items.CompanyID, dbo.vw_Items.CompanyCode, dbo.vw_Items.CompanyName, dbo.vw_Items.CompanyNameUrdu, dbo.vw_Items.RackID, 
                      dbo.vw_Items.RackCode, dbo.vw_Items.RackName, dbo.vw_Items.RackNameUrdu, dbo.vw_Items.InUnit, dbo.OrderDetails.PSize, 
                      dbo.OrderDetails.Packs, dbo.OrderDetails.Lose, dbo.OrderDetails.Qty, dbo.OrderDetails.PackRate, dbo.OrderDetails.Rate, 
                      dbo.OrderDetails.Amount
FROM         dbo.Accounts RIGHT OUTER JOIN
                      dbo.Employees RIGHT OUTER JOIN
                      dbo.vw_Items RIGHT OUTER JOIN
                      dbo.OrderDetails INNER JOIN
                      dbo.Orders ON dbo.OrderDetails.OrderID = dbo.Orders.OrderID ON dbo.vw_Items.ItemID = dbo.OrderDetails.ItemID LEFT OUTER JOIN
                      dbo.Employees AS Employees_1 ON dbo.Orders.ModifyID = Employees_1.EmployeeID ON 
                      dbo.Employees.EmployeeID = dbo.Orders.LoginID LEFT OUTER JOIN
                      dbo.Accounts AS Accounts_1 ON dbo.Orders.BookedByID = Accounts_1.AccountNo ON dbo.Accounts.AccountNo = dbo.Orders.PartyID LEFT OUTER JOIN
                      dbo.SessionInfo ON dbo.Orders.SessionID = dbo.SessionInfo.SessionID

GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane1', @value=N'[0E232FF0-B466-11cf-A24F-00AA00A3EFFF, 1.00]
Begin DesignProperties = 
   Begin PaneConfigurations = 
      Begin PaneConfiguration = 0
         NumPanes = 4
         Configuration = "(H (1[40] 4[20] 2[20] 3) )"
      End
      Begin PaneConfiguration = 1
         NumPanes = 3
         Configuration = "(H (1 [50] 4 [25] 3))"
      End
      Begin PaneConfiguration = 2
         NumPanes = 3
         Configuration = "(H (1 [50] 2 [25] 3))"
      End
      Begin PaneConfiguration = 3
         NumPanes = 3
         Configuration = "(H (4 [30] 2 [40] 3))"
      End
      Begin PaneConfiguration = 4
         NumPanes = 2
         Configuration = "(H (1 [56] 3))"
      End
      Begin PaneConfiguration = 5
         NumPanes = 2
         Configuration = "(H (2 [66] 3))"
      End
      Begin PaneConfiguration = 6
         NumPanes = 2
         Configuration = "(H (4 [50] 3))"
      End
      Begin PaneConfiguration = 7
         NumPanes = 1
         Configuration = "(V (3))"
      End
      Begin PaneConfiguration = 8
         NumPanes = 3
         Configuration = "(H (1[56] 4[18] 2) )"
      End
      Begin PaneConfiguration = 9
         NumPanes = 2
         Configuration = "(H (1 [75] 4))"
      End
      Begin PaneConfiguration = 10
         NumPanes = 2
         Configuration = "(H (1[66] 2) )"
      End
      Begin PaneConfiguration = 11
         NumPanes = 2
         Configuration = "(H (4 [60] 2))"
      End
      Begin PaneConfiguration = 12
         NumPanes = 1
         Configuration = "(H (1) )"
      End
      Begin PaneConfiguration = 13
         NumPanes = 1
         Configuration = "(V (4))"
      End
      Begin PaneConfiguration = 14
         NumPanes = 1
         Configuration = "(V (2))"
      End
      ActivePaneConfig = 0
   End
   Begin DiagramPane = 
      Begin Origin = 
         Top = 0
         Left = 0
      End
      Begin Tables = 
         Begin Table = "OrderDetails"
            Begin Extent = 
               Top = 7
               Left = 473
               Bottom = 259
               Right = 624
            End
            DisplayFlags = 280
            TopColumn = 0
         End
         Begin Table = "Orders"
            Begin Extent = 
               Top = 0
               Left = 0
               Bottom = 259
               Right = 161
            End
            DisplayFlags = 280
            TopColumn = 1
         End
         Begin Table = "vw_Items"
            Begin Extent = 
               Top = 0
               Left = 871
               Bottom = 259
               Right = 1046
            End
            DisplayFlags = 280
            TopColumn = 6
         End
         Begin Table = "Accounts_1"
            Begin Extent = 
               Top = 108
               Left = 197
               Bottom = 222
               Right = 374
            End
            DisplayFlags = 344
            TopColumn = 13
         End
         Begin Table = "Accounts"
            Begin Extent = 
               Top = 65
               Left = 203
               Bottom = 218
               Right = 380
            End
            DisplayFlags = 344
            TopColumn = 11
         End
         Begin Table = "Employees_1"
            Begin Extent = 
               Top = 170
               Left = 218
               Bottom = 284
               Right = 371
            End
            DisplayFlags = 344
            TopColumn = 0
         End
         Begin Table = "Employees"
            Begin Extent = 
               Top = 138
               Left = 216
               Bottom = 252
               Right = 369
            End
            Dis' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'VW_Orders'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPane2', @value=N'playFlags = 344
            TopColumn = 0
         End
         Begin Table = "SessionInfo"
            Begin Extent = 
               Top = 11
               Left = 196
               Bottom = 243
               Right = 349
            End
            DisplayFlags = 344
            TopColumn = 0
         End
      End
   End
   Begin SQLPane = 
   End
   Begin DataPane = 
      Begin ParameterDefaults = ""
      End
      Begin ColumnWidths = 17
         Width = 284
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
         Width = 1500
      End
   End
   Begin CriteriaPane = 
      Begin ColumnWidths = 11
         Column = 1440
         Alias = 900
         Table = 1170
         Output = 720
         Append = 1400
         NewValue = 1170
         SortType = 1350
         SortOrder = 1410
         GroupBy = 1350
         Filter = 1350
         Or = 1350
         Or = 1350
         Or = 1350
      End
   End
End
' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'VW_Orders'
GO
EXEC sys.sp_addextendedproperty @name=N'MS_DiagramPaneCount', @value=2 , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'VIEW',@level1name=N'VW_Orders'

